#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {

        sort(intervals.begin(), intervals.end());
        vector<vector<int>>ans;
        for (vector<int> xy : intervals)
        {
            if (ans.empty() || ans.back()[1] < xy[0])ans.push_back(xy);
            else ans.back()[1] = xy[1] > ans.back()[1] ? xy[1] : ans.back()[1];
        }
        return ans;
    }
};